import { AbstractPlantStruct } from "./AbstractPlantStruct";
import { PlanetProduct } from "./PlanetProduct";
import type { PlanetPin } from "@scvzerng/esi-163/dist/types/esi/plants/model/PlanetLayout";
import type { UniverseType } from "../../../sde/entity";

export class PlanetSender extends AbstractPlantStruct {
  /**
   * 发射中心的容积
   */
  volume = 10000;
  /**
   * 发射器存储设施内容
   */
  contents: PlanetProduct[];
  constructor(pin: PlanetPin, type: UniverseType) {
    super(pin, type);
    this.contents = pin.contents?.map((e: any) => new PlanetProduct(e.amount, e.type)) ?? [];
  }

  /**
   * 计算出需要添加的清单
   * @param inputs
   */
  calcDetails(inputs: PlanetProduct[]): PlanetProduct[] {
    let totalCycleVolume = 0;

    // 首先计算每个周期的总输入体积 (立方米 m³)
    for (const product of inputs) {
      const cycleCount = Math.abs(product.cycleCount ?? 0);
      const unitVolume = product.type.volume;

      if (cycleCount && unitVolume > 0) {
        totalCycleVolume += unitVolume * cycleCount;
      }
    }

    // 计算最大可执行的完整周期数，并确保它是整数
    const maxCycles = Math.floor(this.volume / totalCycleVolume);

    const result: PlanetProduct[] = [];

    // 根据最大周期数计算每个产品的总体积和总数量
    for (const product of inputs) {
      const cycleCount = Math.abs(product.cycleCount ?? 0);

      if (cycleCount) {
        const totalProductCount = cycleCount * maxCycles;

        // 使用构造函数创建新的 PlanetProduct 对象
        const outputProduct = new PlanetProduct(totalProductCount, product.type, cycleCount);
        result.push(outputProduct);
      }
    }

    return result;
  }
}
